/**
 * This file is part of Megasquirt Logger for PalmOS.
 * Copyright (C) 2011 Andrew Beard
 *
 * Megasquirt Logger is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Megasquirt Logger is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Megasquirt Logger.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "SelectedLogDatabase.h"

#include <DataMgr.h>
#include "ExportDatabase.h"

/**
 * Constructor for the SelectedLogDatabase class.  Sets the m_LastError
 *   variable if the database can't be found.
 *
 * @param[in] pDatabaseName ASCII string holding the name of the database to
 *                            select.
 */
SelectedLogDatabase::SelectedLogDatabase( const Char* pDatabaseName ) :
    m_LastError( errNone )
{
    StrCopy( m_DatabaseName,
             pDatabaseName );

    m_DatabaseID = DmFindDatabase( 0,
                                   m_DatabaseName );
    if ( m_DatabaseID == 0 )
    {
        m_LastError = DmGetLastErr();
    }
}

/**
 * Destructor for the SelectedLogDatabase class.
 */
SelectedLogDatabase::~SelectedLogDatabase()
{

}

/**
 * Copies the selected database to a memory card.
 *
 * @param[in] nRealTimeUpdateLength Size of a single output record payload from
 *                                    the MegaSquirt.
 */
void SelectedLogDatabase::CopyToCard( UInt16 nRealTimeUpdateLength )
{
    ExportDatabase* pExport = NULL;

    pExport = new ExportDatabase( m_DatabaseID );
    pExport->WriteToFilesystem( nRealTimeUpdateLength );
    delete pExport;
    pExport = NULL;
}

/**
 * Deletes the selected database.  The object should be destroyed after calling
 *   this method as the database no longer exists.
 */
void SelectedLogDatabase::Delete( void )
{
    if ( m_DatabaseID != 0 )
    {
        DmDeleteDatabase( 0,
                          m_DatabaseID );
        m_DatabaseID = 0;
    }
}

/**
 * Returns the number of records in the selected database.
 *
 * @return The number of records in the selected database, or 0xFFFF if
 *           the database could not be found or has been deleted.
 */
UInt32 SelectedLogDatabase::GetNumRecords( void )
{
    UInt32 nNumRecords = 0xFFFF;

    if ( m_DatabaseID != 0 )
    {
        DmDatabaseSize( 0,
                        m_DatabaseID,
                        &nNumRecords,
                        NULL,
                        NULL );
    }
    return nNumRecords;
}

/**
 * Returns the name of the selected database.
 *
 * @return Constant ASCII string with the name of the database.
 */
const Char* SelectedLogDatabase::GetName( void ) const
{
    return m_DatabaseName;
}

/**
 * Returns the last error encountered by the object.
 *
 * @return PalmOS error code with the last error encountered.
 */
Err SelectedLogDatabase::GetLastError( void ) const
{
    return m_LastError;
}